package com.njwb.reflect;

/**
 * Description: 类的描述
 * Author: yuandeyin@itany.com
 * Date: 2025-08-07 11:31
 * Version: 1.0
 */
public class TestReflect {
    public static void main(String[] args) {
        //得到Class对象
        //方法1.
//        try {
//            //forName:加载一个类
//            Class personClass = Class.forName("com.njwb.reflect.Person");
//            System.out.println(personClass);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
        //方法二：
//        Person person = new Person();
//        Class personClass = person.getClass();
//        System.out.println(personClass);

        //方法三：
//        Class personClass = Person.class;
//        System.out.println(personClass);

        //反射实例化对象
        try {
            Class personClass = Class.forName("com.njwb.reflect.Person");
            Person person = (Person) personClass.newInstance();
            System.out.println(person);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
